package com.yzh.wanandroid.adapter

import android.annotation.SuppressLint
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.paging.LoadState
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.yzh.wanandroid.R
import com.yzh.wanandroid.databinding.ItemListBinding
import com.yzh.wanandroid.databinding.ItemTopViewpagerBinding
import com.yzh.wanandroid.model.RefreshData
import com.yzh.wanandroid.network.retrofit.responsebean.ListData
import com.yzh.wanandroid.network.retrofit.responsebean.TopBannerData

class ArticleListAdapter :
    PagingDataAdapter<ListData, RecyclerView.ViewHolder>(ARTICLE_DIFF_CALLBACK) {

    private val TAG = "ArticleListAdapter"

    /** banner 界面 **/
    private val BANNER_ITEM = 0

    /** 列表界面 **/
    private val ARTICLE_LIST_ITEM = 1

    private lateinit var mData: List<TopBannerData>

    var refreshData = RefreshData()

    init {
        this.addLoadStateListener {
            when (it.refresh) {
                is LoadState.NotLoading -> refreshData.isfreshing = false
                is LoadState.Loading -> refreshData.isfreshing = true
                is LoadState.Error -> refreshData.isfreshing = false
                else -> refreshData.isfreshing = false
            }
        }
    }

    /** 文章列表点击事件监听 **/
    private lateinit var mOnClickListener: (View, String, String) -> Unit

    fun setOnClickListener(onClickListener: (View, String, String) -> Unit) {
        this.mOnClickListener = onClickListener
    }

    override fun getItemViewType(position: Int): Int {
        if (position == 0) {
            return BANNER_ITEM
        }
        return ARTICLE_LIST_ITEM
    }

    /**
     * 设置banner数据
     */
    fun setBannerData(data: List<TopBannerData>) {
        this.mData = data
        this.notifyDataSetChanged()
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (getItemViewType(position) == BANNER_ITEM) {
            if (holder is ArticleListAdapter.BannerViewHolder) {
                if (this::mData.isInitialized) {
                    val adapter = HomeBannerAdapter(mData)
                    holder.bind(adapter)
                }
            }
        } else {
            if (holder is ArticleListAdapter.ArticleListViewHolder) {
                holder.bind(getItem(position))
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        if (viewType == BANNER_ITEM) {
            val bannerBinding: ItemTopViewpagerBinding =
                DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context),
                    R.layout.item_top_viewpager,
                    parent,
                    false
                )
            return BannerViewHolder(bannerBinding)
        } else {
            val articleListBinding: ItemListBinding =
                DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context),
                    R.layout.item_list,
                    parent,
                    false
                )
            return ArticleListViewHolder(articleListBinding)
        }
    }

    inner class ArticleListViewHolder(private val binding: ItemListBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bind(data: ListData?) {
            binding.run {
                model = data
                data?.let {
                    cardView.setOnClickListener { view ->
                        mOnClickListener.invoke(view, it.link, it.title)
                    }
                    executePendingBindings()
                }
            }
        }
    }

    inner class BannerViewHolder(private val binding: ItemTopViewpagerBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bind(adapter: HomeBannerAdapter?) {
            binding.run {
                this.adapter = adapter
                this.adapter?.setOnBannerClickListener { view, url, title ->
                    mOnClickListener.invoke(view, url, title)
                    Log.i(TAG,"url = $url")
                    executePendingBindings()
                }
            }
        }
    }

    companion object {
        private val ARTICLE_DIFF_CALLBACK: DiffUtil.ItemCallback<ListData> =
            object : DiffUtil.ItemCallback<ListData>() {
                override fun areItemsTheSame(
                    oldResult: ListData,
                    newResult: ListData
                ): Boolean {
                    return oldResult.id == newResult.id
                }

                @SuppressLint("DiffUtilEquals")
                override fun areContentsTheSame(
                    oldResult: ListData,
                    newResult: ListData
                ): Boolean {
                    return oldResult == newResult
                }
            }
    }
}
